import { createStore } from 'vuex'

export default createStore({
  state: {
    info: localStorage.info ? JSON.parse(localStorage.info) : []
  },
  getters: {
  },
  mutations: {
    // 保存到vuex
    saveData ( state, payload ) {
      console.log(payload);
      let index = state.info.findIndex( item => item.id == payload.id)
      if ( index == -1 ) {
        state.info.push( payload );
      } else {
        state.info[ index ].num++;
      }

      // 本地存储
      localStorage.setItem('info', JSON.stringify(state.info))
    }
  },
  actions: {
  },
  modules: {
  }
})
